class ItemMenusController < ApplicationController
  # GET /item_menus
  # GET /item_menus.json
  def index
    @item_menus = ItemMenu.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @item_menus }
    end
  end

  # GET /item_menus/1
  # GET /item_menus/1.json
  def show
    @item_menu = ItemMenu.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @item_menu }
    end
  end

  # GET /item_menus/new
  # GET /item_menus/new.json
  def new
    @item_menu = ItemMenu.new
    @menus = Menu.where ativo: true
    @item_menus = ItemMenu.all
    @acao_item_menus = AcaoItemMenu.all

#TODO FILTRAR PAI DE ACORDO COM MENU SELECIONADO || SELECIONAR MENU AO SELECIONAR PAI

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @item_menu }
    end
  end

  # GET /item_menus/1/edit
  def edit
    @item_menu = ItemMenu.find(params[:id])
    @menus = Menu.where ativo: true
    @item_menus = ItemMenu.where "id != " + params[:id]
    @acao_item_menus = AcaoItemMenu.all
  end

  # POST /item_menus
  # POST /item_menus.json
  def create
    @item_menu = ItemMenu.new(params[:item_menu])

    respond_to do |format|
      if @item_menu.save
        format.html { redirect_to @item_menu, notice: 'Item criado com sucesso!' }
        format.json { render json: @item_menu, status: :created, location: @item_menu }
      else
        format.html { render action: "new" }
        format.json { render json: @item_menu.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /item_menus/1
  # PUT /item_menus/1.json
  def update
    @item_menu = ItemMenu.find(params[:id])

    respond_to do |format|
      if @item_menu.update_attributes(params[:item_menu])
        format.html { redirect_to @item_menu, notice: 'Item alterado com sucesso!' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @item_menu.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /item_menus/1
  # DELETE /item_menus/1.json
  def destroy
    @item_menu = ItemMenu.find(params[:id])
    @item_menu.destroy

    respond_to do |format|
      format.html { redirect_to item_menus_url }
      format.json { head :no_content }
    end
  end

end
